/*
 * This file is part of Jeve.

    Jeve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Jeve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2007 Jochen Bedersdorfer (beders@yahoo.com)
 *
 */
package de.monoid.eve;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import static de.monoid.eve.Validator.*;
/** 
 * Models an eve character.
 * @author beders
 *
 */
public class Character {

  private String id;

  private Document skillInTraining;

  private EveInterface eveInterface;

  /**
   * Create a character object, use the given eve interface to populate the info
   * @param anInterface the interface to use
   * @param anId the character id as given on the eve website
   */
  Character(EveInterface anInterface, String anId) {
    if (anId == null || anInterface == null)
      throw new IllegalArgumentException("Both interface and id must be set! " + anInterface + ","
          + anId);
    id = anId;
    eveInterface = anInterface;
  }

  /** Gets the training time of the currently learned skill of this character.
   * 
   * @return the training time in milliseconds, 0 is skill has been learned (and eve api is still reporting it)
   *  or -1 if there is no skill currently trained
   */
  public long getRemainingTrainingTime() {
    long retVal = -1; // -1 means: no training underway
    // replace with depedency injection later
    if (skillInTraining == null || !valid(skillInTraining)) {
      skillInTraining = eveInterface.getSkillsInTraining(this);
    }
    if (skillInTraining == null) throw new IllegalStateException("Service is not available");
    XPath xpath = XPathFactory.newInstance().newXPath();
    try {
      String endTime = xpath.evaluate("/eveapi/result/trainingEndTime", skillInTraining);
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
      format.setCalendar(cal);
      Date date = format.parse(endTime);
      retVal = Math.max(date.getTime() - System.currentTimeMillis(), 0); 
    } catch (XPathExpressionException e) {  
      e.printStackTrace();
      // no skill in training
    } catch (ParseException e) {
      // xml content is malformed - no skill in training
      e.printStackTrace();
    }
    return retVal;
  }


  /**
   * Get the character ID
   * @return the character id
   */
  public String getID() {
    return id;
  }

}
